#include "process.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#define NUM 101
#define STYLE '='
#define POINT '.'
#define SPACE ' '
const int pnum = 6;

//version2:进度条应该根据具体的下载量动态刷新
void Process(double total, double current)
{
  //更新出当前进度的百分比
  double rate = (current/total)*100;
  printf("test:%.1lf%%\r",rate);
  fflush(stdout);

  //更新进度条主体
  char bar[NUM];
  int i;
  memset(bar,'\0',sizeof(bar));
  for(i =0; i < (int)rate; i++)
  {
    bar[i] = STYLE;
  }
  
  //更新光标
  static int num = 0;
  num++;
  num%=pnum;

  char points[pnum+1];
  memset(points,'\0',sizeof(points));

  for(i = 0; i < pnum; i++)
  {
    if(i < num) points[i] = POINT;
    else
    points[i] = SPACE;  
  }
  for(i = 0; i < num; i++)
  {
    points[i] = POINT;
  }
  printf("[%-100s][%.1lf%%]%s\r",bar,rate,points);
  fflush(stdout);
}

////version 1
//void Process()
//{
//  char bar[NUM];
//  const char* lable = "|/-\\";
//  int len = strlen(lable);
//  memset(bar,'\0',sizeof(bar));
//  int cnt = 0;
//  while(cnt <= 100)
//  {
//    printf("[%-100s][%d%%][%c]\r",bar,cnt,lable[cnt%len]);
//    fflush(stdout);
//    bar[cnt] = STYLE;
//    cnt++;
//    if(cnt != NUM)
//    bar[cnt]='>';
//    else
//      bar[cnt] = '=';
//    usleep(10000);
//  }
//  printf("\r\n");
//}
//
